package com.atguigu.eduservice.controller;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduChapter;

import com.atguigu.eduservice.service.impl.EduChapterService;
import io.swagger.annotations.Api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author cyt
 * @create 2020-05-09 8:35
 */

@Api(description = "章节管理")
@RestController
@RequestMapping("/eduservice/chapter")

public class EduChapterController {


    @Autowired
    EduChapterService eduChapterService;

    // 添加章节
    @PostMapping("/saveChapter")
    public R saveChapter(@RequestBody EduChapter chapterVo) {

        System.out.println(chapterVo);
        eduChapterService.save(chapterVo);
        return R.ok();
    }

    // 删除章节
    @DeleteMapping("/deleteChapter/{id}")
    public R deleteChapter(@PathVariable String id) {
        boolean deleteChapter = eduChapterService.deleteChapter(id);
        if (deleteChapter) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    // 修该章节
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter chapterVo) {
        eduChapterService.updateById(chapterVo);
        return R.ok();
    }

    // 查询章节
    @GetMapping("/getChapter/{id}")
    public R getChapter(@PathVariable String id) {
        System.out.println(id);
        EduChapter chapterById = eduChapterService.getChapterById(id);
        System.out.println(chapterById);
        return R.ok().data("chapter", chapterById);
    }
}
